
<script type="text/javascript">
    $(document).ready(function(){
        $('#bt_add_menufacturer').click(function(){
            var name = $('#tb_manufacturer_name').val();
            var description = $('#tb_menufacturer_description').val();
            var category = $('#category_uri').val();
            $.post('~admin/addManufacturer',{ name : name , description : description, category : category}, function(data){
                if(data.indexOf('FAIL') == -1){
                    $.alertBox('THÔNG BÁO', 'Thêm mới nhà sản xuất thành công!');
                    var sHtml = 
                    '<tr>' + 
                    '   <td>'+ data +'</td>' + 
                    '   <td>'+ name +'</td>' + 
                    '   <td>'+ $('#category_name').val() +'</td>' + 
                    '   <td>'+ description +'</td>' + 
                    '   <td><input type="button" value="Edit" class="link_button bt_edit_menufacturer"></td>' + 
                    '</tr>';
                    $('#table_menufacturer tbody').prepend(sHtml);
                    $('#tb_manufacturer_name').val('');
                    $('#tb_menufacturer_description').val('');
                }
            });
        });

        $('td').click(function(event){            
            CURRENT_CELL = $(this);
            $.editProperty($(this).attr('data'),$(this),event);
        });
        $(document).click(function(){
            if(!$('#form_edit_property').is(':hover')){
                $('#form_edit_property').css('display','none');
            }
        });

        $('#bt_update_property').click(function(){
            var option = $(this).attr('data').split('|');
            if(option[0] == 'text' || option[0] == 'longtext'){
                var uri = option[1];
                var property = option[2];
                var value = $('.prop_val').val();
            }
            else if(option[0] == 'list'){
                var uri = option[2];
                var property = option[3];
                var title = $('#form_edit_property .custom_combobox_title').val();
                var value = $('#form_edit_property .custom_combobox_val').val();
            }
            $.post('~admin/updateProperty',{uri : uri , property : property , value : value},function(data){
                $('#form_edit_property').css('display','none');
                $.alertBox('THÔNG BÁO', 'Cập nhập thành công!');
                if(option[0] == 'text' || option[0] == 'longtext'){
                    CURRENT_CELL.text(value);
                }
                else if(option[0] == 'list'){
                    CURRENT_CELL.text(title);
                }
            });
        });
    });
</script>
<div class="admin_header_title">MENUFACTURERS MANAGE</div>
<div class="metro_table green_metro_style">
    <div class="metro_table_title"><i class="fa fa-bars"></i> Add a New Menufacturer</div>
    <div class="metro_table_content">
        <div style="padding: 20px;">
            <div>
                <input style="width: 150px;margin-right: -8px;" type="text" value="Ten Nhà Sản Xuất" disabled="disabled"> 
                <input id="tb_manufacturer_name" type="text" style="width: 300px; padding-right: 30px; ">
            </div>
            <?php 
                $aParams = array(
                    'width' => '465px',
                );
                includeBlock('block/categoryBlock' , $aParams);
            ?>
            <div>
                <h2 class="small_title">Mô tả</h2>
                <textarea id="tb_menufacturer_description" cols="" rows="" style="width: 510px;height:140px;"></textarea>
            </div>
            <div style="text-align: right;width:510px;margin-top:20px;">
                <button id="bt_add_menufacturer" class="flat_metro_button green_metro_style">Add a New Menufacturer <i class="fa fa-plus"></i></button>
            </div>
        </div>
    </div>
</div>
<!--<h2 class="title green_style">DANH SÁCH NHÀ SẢN XUẤT</h2>-->
<div class="metro_table orange_metro_style">
    <div class="metro_table_title"><i class="fa fa-bars"></i> List Menufactures</div>
    <div class="metro_table_content">
        <table id="table_menufacturer">
            <thead>
                <tr>
                    <th><i class="fa fa-tag"></i> ID Nhà Sản Xuất</th>
                    <th><i class="fa fa-info"></i> Tên Nhà Sản Xuất</th>
                    <th><i class="fa fa-th"></i> Danh Mục</th>
                    <th><i class="fa fa-info"></i> Mô tả</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php
                    if($template->get('aManufacturers'))
                    {
                        foreach($template->get('aManufacturers') as $aManufacturer)
                        {
                            echo '<tr val="'.$aManufacturer['uri'].'">';
                            echo '<td><a href="'.$aManufacturer['uri'].'">'.end(explode('/',$aManufacturer['uri'])).'<a></td>';
                            echo '<td data="text|'.$aManufacturer['uri'].'|bus:name'.'">'.$aManufacturer['name'].'</td>';
                            echo '<td data="list|cbcategory|'.$aManufacturer['uri'].'|bus:hasCategory'.'">'.(isset($aManufacturer['categoryName']) ? $aManufacturer['categoryName'] : '').'</td>';
                            echo '<td data="longtext|'.$aManufacturer['uri'].'|bus:description'.'">'.$aManufacturer['description'].'</td>';
                            echo '<td><button class="table_bt_metro_edit bt_edit_menufacturer"><i class="fa fa-pencil"></i></button><button class="table_bt_metro_delete"><i class="fa fa-trash-o"></i></button></td>';
                            echo '</tr>';
                        }
                    }
                ?>
            </tbody>
        </table>
    </div>
</div>

<div id="form_edit_property" class="edit_property_form">
    <div class="form_edit_content">
    </div>
    <div style="margin-top:10px;text-align: right;">
        <button id="bt_update_property" class="table_bt_metro_edit bt_edit_menufacturer green_metro_style"><i class="fa fa-check"></i></button>
    </div>
</div>

<div style="display: none;">
    <?php
        $aParamLocation = array(
            'id' => 'cbcategory',
            'aValue' => $template->get('aCategories'),
            'stringTitle' => 'name',
            'stringKey' => 'uri'
        );
        includeBlock('block/customComboboxBlock',$aParamLocation);
    ?>
</div>